package com.lzh.farmersshop.controller.mall;


import com.lzh.farmersshop.entity.Evaluation;
import com.lzh.farmersshop.entity.Order;
import com.lzh.farmersshop.entity.Userinfo;
import com.lzh.farmersshop.service.EvaluationService;
import com.lzh.farmersshop.service.OrderService;
import com.lzh.farmersshop.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * (Evaluation)表控制层
 *
 * @author makejava
 * @since 2024-01-04 15:41:50
 */
@Slf4j
@RestController
@RequestMapping("mall/evaluation")
@Transactional(rollbackFor = Exception.class)
public class UserEvaluationController {
    /**
     * 服务对象
     */
    @Resource
    private EvaluationService evaluationService;
    @Resource
    private OrderService orderService;




    /**
     * 新增数据
     *
     * @param evaluation 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public Result insert(HttpServletRequest request, Evaluation evaluation, @RequestParam(value = "ids[]",required = false) Integer[] ids,Integer orderid) {
        //获取用户信息
        if (request.getSession().getAttribute("userinfo")== null) {
            return Result.failure(401,"请先登录！！！");
        }

        if (ids==null){
            return Result.failure("请选择商品！！！");
        }

        if (evaluation==null){
            return Result.failure("请填写评价！！！");
        }

        Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");
        Date date = new Date();
        //循环ids
        for (Integer id : ids) {
            Evaluation one = new Evaluation();
            one.setScore(evaluation.getScore());
            one.setState(evaluation.getState());
            one.setContent(evaluation.getContent());
            one.setUserid(userinfo.getId());
            one.setGoodsid(id);
            one.setTime(date);
            one.setIsdelete(1);
            evaluationService.save(one);
        }

        //修改订单状态
        Order order = new Order();
        order.setId(orderid);
        order.setState(5);
        if (orderService.updateById(order)){
            return Result.success("评价成功！！！");
        }
        return Result.failure("评价失败！！！");
    }


}

